﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
  
    public partial class 出院 : Form
    {
      
        public 出院()
        {
            InitializeComponent();
        }
        public void Fill()
        {

            string sql = "select InHospital.HosNo,Bed.BedNo,PatName,PatSex,PatType,HosSurgerydate,HosDepartment,InHospital.HosArea from InHospital,Bed,Patient where InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";
           
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                SqlDataAdapter da = new SqlDataAdapter();
                    DataSet ds = new DataSet();
                da = new SqlDataAdapter(sql,conn );
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
        }

        private void 出院_Load(object sender, EventArgs e)
        {
            Fill();
        }

        private void 取消_Click(object sender, EventArgs e)
        {
            this.Hide();
           
        }

        private void bt_ss_Click(object sender, EventArgs e)
        {
            string sql = "select InHospital.HosNo,Bed.BedNo,PatName,PatSex,PatType,HosSurgerydate,HosDepartment,HosArea from  InHospital,Bed,Patient where  InHospital.HosNo=Patient.HosNo and  InHospital.BedNo=Bed.BedNo";
            string i = tb_ZYNo.Text.Trim();
            if (i != "")
                sql += " and  InHospital.HosNo='" + i + "'";
            else
                MessageBox.Show("搜索内容不能为空！");
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da = new SqlDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
        }

        private void 确定_Click(object sender, EventArgs e)
        {
            
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string a = Convert.ToString(this.dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
                string b = Convert.ToString(this.dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value).Trim();
                if (MessageBox.Show("确定要删除吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                    return;
                string str = string.Format("insert into LeaveHospital( HosNo, BedNo, HosIndate, HosDepartment, HosPrepay, PatType, HosSurgerydate, HosRank, HosCondition, HosIllness,HosTotal,HosBalance,DocNo,NurNo,HosOfficeDate,HosArea)select HosNo, BedNo, HosIndate, HosDepartment, HosPrepay, PatType, HosSurgerydate, HosRank, HosCondition, HosIllness,HosTotal,HosBalance,DocNo,NurNo,HosOfficeDate,HosArea from InHospital where InHospital.HosNo='" + a+ "'");

                string str1 = string.Format("delete from InHospital  where InHospital.HosNo='" + a + "'");
                string ssql = string.Format("update Bed set BedCondition='未用'where Bed.BedNo='" + b + "'");
                string sql1 = string.Format("update Patient set Condition='出院'where Patient.HosNo='" + a+ "'");
                
                SqlCommand com = new SqlCommand(str, conn);
                SqlCommand bed = new SqlCommand(ssql, conn);
              
                SqlCommand dein = new SqlCommand(str1, conn);
                SqlCommand pat = new SqlCommand(sql1, conn);
                DateTime dt = System.DateTime.Now;
                string upl = string.Format("update LeaveHospital set LeaveHospitalData='" + dt + "'where HosNo='" + a + "'");
                SqlCommand ldata = new SqlCommand(upl , conn);
                 conn.Open();
                 
                int m = Convert.ToInt32(com.ExecuteNonQuery());
                int n = Convert.ToInt32(dein.ExecuteNonQuery());
                int x = Convert.ToInt32(bed.ExecuteNonQuery());
                int y = Convert.ToInt32(pat.ExecuteNonQuery());
                int z = Convert.ToInt32(ldata.ExecuteNonQuery());
                if (n == 1 && m == 1 && x == 1&&y==1&&z==1)
                {
                    MessageBox.Show("出院办理成功！");
                }
                else
                {
                    MessageBox.Show("出院办理失败！");
                }
                
                DataGridViewSelectedRowCollection mRowlnfo = this.dataGridView1.SelectedRows;
                foreach (DataGridViewRow dataGridViewRow in mRowlnfo)
                {
                    this.dataGridView1.Rows.RemoveAt(dataGridViewRow.Index);
                    dataGridViewRow.Selected = false;
                }
                this.dataGridView1.Refresh();
            }
           
        }
       
        
    }
}
